package fyp.spedmew;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.app.ListFragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.view.View.OnClickListener;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import fyp.spedmew.backend.entity.jobEndpoint.model.Job;
import fyp.spedmew.backend.entity.userEndpoint.model.User;
import fyp.spedmew.connector.DownloadImageATask;
import fyp.spedmew.connector.JobListATask;
import fyp.spedmew.connector.UserFindByIdATask;
/**
 * Created by chengys
 */


public class UserListActionBarActivity extends ActionBarActivity {

    ImageButton imageButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_of_shipment);
        initActionBar();



        final FloatingActionButtonView fabButton = new FloatingActionButtonView.Builder(this)
                .withDrawable(getResources().getDrawable(R.drawable.c))
                .withGravity(Gravity.BOTTOM | Gravity.RIGHT)
                .withMargins(0, 0, 10, 10)
                .create();

        fabButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

               fabButton.hideFloatingActionButton();
                Intent intent = new Intent(UserListActionBarActivity.this,CreateJobInitStageActionBarActivity.class);
                startActivity(intent);

        }
        });
    }

    @SuppressWarnings("deprecation")
    private void initActionBar() {
        final listRecpFragment Recplist = new listRecpFragment();
        final listShipFragment Shiplist = new listShipFragment();
        if (getSupportActionBar() != null) {
            ActionBar actionBar = getSupportActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            actionBar.addTab(actionBar.newTab()
                    .setText("Shipment List")
                    .setTabListener(new ActionBar.TabListener() {
                        @Override
                        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
                            if (getFragmentManager().findFragmentById(R.id.senderView_frame) == null) {

                             getFragmentManager().beginTransaction().add(R.id.senderView_frame, Shiplist).commit();
                            };


                        }

                        @Override
                        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
                        }

                        @Override
                        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
                        }
                    }));
            actionBar.addTab(actionBar.newTab()
                    .setText("Recipient List")
                    .setTabListener(new ActionBar.TabListener() {
                        @Override
                        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
                            if (getFragmentManager().findFragmentById(R.id.senderView_frame) != null) {

                                getFragmentManager().beginTransaction().remove(Shiplist);
                                getFragmentManager().beginTransaction().replace(R.id.senderView_frame, Recplist).commit();

                            };

                        }

                        @Override
                        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
                            if (getFragmentManager().findFragmentById(R.id.senderView_frame) != null) {

                                getFragmentManager().beginTransaction().remove(Shiplist);

                            };

                        }

                        @Override
                        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
//                            if (getFragmentManager().findFragmentById(R.id.senderView_frame) == null) {
//
//                                getFragmentManager().beginTransaction().replace(R.id.senderView_frame, Recplist).commit();
//                            };

                        }
                    }));

        }
    }



    public static class listShipFragment extends ListFragment {

            private List<ListViewClass> mItems;        // ListView items list
            private ImageView ImgView;
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                // initialize the items list
                mItems = new ArrayList<ListViewClass>();

                Resources resources = getResources();
                try {
                    List<Job> list;
                    list = new JobListATask().execute().get();

                    {
                        for (Job job : list) {
                            User dispatcher = new UserFindByIdATask().execute(job.getRecipientId()).get();

                            ListViewClass item = new ListViewClass();
                            item.setJobObject(job);
                            item.setTitle(job.getTitle());
                            item.setDescription(job.getDetail());
                            item.setName(dispatcher.getName());
                            item.setPrice("$ " + job.getPrice());
                            item.setIcon(resources.getDrawable(R.drawable.coffee));
                            item.setStatus_icon(resources.getDrawable(R.drawable.green));
                            item.setPic(job.getTitle());
                            item.setStatus("Status");
                            item.setAlertIcon(resources.getDrawable(R.drawable.ic_action_alert));
                            mItems.add(item);
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                } catch (NullPointerException n){
                    ListViewClass item = new ListViewClass();
                }

                // initialize and set the list adapter
                setListAdapter(new ListViewArrayAdapter(getActivity(), mItems));
            }





        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            // remove the dividers from the ListView of the ListFragment
            getListView().setDivider(null);


        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            // retrieve theListView item
            ListViewClass item = mItems.get(position);
             User recipient = new User();
            try {
                recipient =  new UserFindByIdATask().execute(item.getJobObject().getRecipientId()).get();
                } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            Bundle jobDetails=new Bundle();
            jobDetails.putString("name", item.getJobObject().getTitle());
            jobDetails.putString("descr", item.getJobObject().getDetail());
            jobDetails.putString("price", item.getJobObject().getPrice());
            jobDetails.putString("customIns",item.getJobObject().getInstruction());
            jobDetails.putString("recipientNo",recipient.getPhone());
            jobDetails.putString("pickupLoc",item.getJobObject().getPickupAddress().getLine1());
            jobDetails.putString("dropoffLoc", item.getJobObject().getDeliverAddress().getLine1());
            jobDetails.putString("date",item.getJobObject().getShipDate().toString());
            jobDetails.putString("id",item.getJobObject().getId().toString());
            jobDetails.putString("imgKey", item.getJobObject().getImgKey());

            Intent activity = new Intent(v.getContext(), JobDetailActionBarActivity.class);
            activity.putExtras(jobDetails);
            startActivity(activity);
        }
    }

    public static class listRecpFragment extends ListFragment {

        private List<ListViewClass> mItems;        // ListView items list

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // initialize the items list
            mItems = new ArrayList<ListViewClass>();
            Resources resources = getResources();
            try {
                List<Job> list;
                list = new JobListATask().execute().get();

                {
                    for (Job job : list) {
                        User dispatcher = new UserFindByIdATask().execute(job.getRecipientId()).get();
                        ListViewClass item = new ListViewClass();
                        item.setJobObject(job);
                        item.setTitle(job.getTitle());
                        item.setDescription(job.getDetail());
                        item.setName(dispatcher.getName());
                        item.setPrice("$ " + job.getPrice());
                        item.setIcon(resources.getDrawable(R.drawable.coffee));
                        item.setStatus_icon(resources.getDrawable(R.drawable.aol));
                        item.setPic(job.getTitle());
                        //item.setAlertIcon(resources.getDrawable(R.drawable.blank));
                        item.setStatus("Status");
                        mItems.add(item);
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (NullPointerException n){
                ListViewClass item = new ListViewClass();
            }

            // initialize and set the list adapter
            setListAdapter(new ListViewArrayAdapter(getActivity(), mItems));
        }



        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            // remove the dividers from the ListView of the ListFragment
            getListView().setDivider(null);
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            // retrieve theListView item
            ListViewClass item = mItems.get(position);
            User recipient = new User();
            try {
                recipient =  new UserFindByIdATask().execute(item.getJobObject().getRecipientId()).get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            Bundle jobDetails=new Bundle();
            jobDetails.putString("name", item.getJobObject().getTitle());
            jobDetails.putString("descr", item.getJobObject().getDetail());
            jobDetails.putString("price", item.getJobObject().getPrice());
            jobDetails.putString("customIns",item.getJobObject().getInstruction());
            jobDetails.putString("recipientNo",recipient.getPhone());
            jobDetails.putString("pickupLoc",item.getJobObject().getPickupAddress().getLine1());
            jobDetails.putString("dropoffLoc", item.getJobObject().getDeliverAddress().getLine1());
            jobDetails.putString("date",item.getJobObject().getShipDate().toString());


            Intent activity = new Intent(v.getContext(),RJobDetailActionBarActivity.class);
            activity.putExtras(jobDetails);
            startActivity(activity);
        }
    }
//    public void addButtonListener(){
//
//        imageButton = (ImageButton) findViewById(R.id.imagebutton);
//        imageButton.setOnClickListener(new OnClickListener(){
//            public void onClick(View v)
//            public void onClick(View v)
//            {
//                Intent intent = new Intent(UserListActionBarActivity.this,CreateJobInitStageActionBarActivity.class);
//                startActivity(intent);
//            }
//        });
//
//    }
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);


    }
}

